package com.mstar.qs.common.jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mstar.qs.common.bean.CommonCompositeMarket;
import com.mstar.qs.common.bean.CommonExchange;
import com.mstar.qs.common.bean.CommonMarket;
import com.mstar.qs.common.bean.CommonSecurity;

public class MarketAndRegionFactory {
	
	public static CommonCompositeMarket buildCompositeMarket() {
		Connection conn=null;
		try {
			conn=JDBCManager.obtainQSDBConnection();
			Map<Integer, CommonExchange> exchs=readMainlyExchangeInfo(conn);
			Map<Integer, CommonSecurity> secus=readSecurities(conn);
			List<CommonMarket> marks=readMarkets(conn);
			return CommonCompositeMarket.buildInstance(exchs, secus, marks);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			if(conn!=null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
		
	}

	private static Map<Integer, CommonExchange> readMainlyExchangeInfo(Connection conn) {
		try {
			String sql = "select * from Exchanges";
			Statement stat = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
			ResultSet result = stat.executeQuery(sql);
			return pricessMainExchanges(result);
		}  catch (SQLException e) {
			e.printStackTrace();
		} 

		return null;
	}

	private static Map<Integer, CommonExchange> pricessMainExchanges(ResultSet result) throws SQLException {
		Map<Integer, CommonExchange> exch = new HashMap<Integer, CommonExchange>();
		while (result.next()) {

			int id = result.getInt("ExchID");
			CommonExchange ex = new CommonExchange(id);
			ex.setExchId(id);
			ex.setShortName(result.getString("ShortName"));
			ex.setTimezoneId(result.getString("TimezoneID"));
			ex.setName(result.getString("Name"));
			ex.setMicCode(result.getString("MicCode"));
			ex.setDescription(result.getString("Description"));
			ex.setRegionName(result.getString("RegionName"));
			ex.setDelayMinute(result.getInt("DelayMinute"));
			exch.put(id, ex);
		}
		if (exch.size() > 0)
			return exch;
		return null;
	}
	

	private static Map<Integer, CommonSecurity> readSecurities(Connection conn) {
		try {
			String sql = "select * from Securities";
			Statement stat = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
			ResultSet result = stat.executeQuery(sql);
			return pricessSecurities(result);
		}  catch (SQLException e) {
			e.printStackTrace();
		} 

		return null;
	}

	private static Map<Integer, CommonSecurity> pricessSecurities(ResultSet result) throws SQLException {
		Map<Integer, CommonSecurity> secs = new HashMap<Integer, CommonSecurity>();
		while (result.next()) {
			
			int id = result.getInt("SecuType");
			CommonSecurity se = new CommonSecurity(id);
			se.setSecuType(id);
			se.setName(result.getString("Name"));
			se.setDescription(result.getString("Description"));
			secs.put(id, se);
		}
		if (secs.size() > 0)
			return secs;
		return null;
	}
	
	private static List<CommonMarket> readMarkets(Connection conn) {
		try {
			String sql = "select * from Markets";
			Statement stat = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
			ResultSet result = stat.executeQuery(sql);
			return pricessMarkets(result);
		}  catch (SQLException e) {
			e.printStackTrace();
		} 

		return null;
	}

	private static List<CommonMarket> pricessMarkets(ResultSet rs) throws SQLException {
		List<CommonMarket> marks = new ArrayList<CommonMarket>();
		while (rs.next()) {
			CommonMarket mar = new CommonMarket(rs.getInt("ExchID"),rs.getInt("SecuType"));

			mar.setDerivedMarkId(rs.getInt("DerivedMarkID"));
			mar.setDerivedSecuType(rs.getInt("DerivedSecuType"));

			mar.setOpenMinute(rs.getInt("OpenMinute"));
			mar.setCloseMinute(rs.getInt("CloseMinute"));
			mar.setPreOpenMinute(rs.getInt("PreOpenMinute"));
			mar.setAfterHourMinute(rs.getInt("AfterHourMinute"));
			mar.setIsInternational(rs.getShort("IsInternational"));
			mar.setMarketName(rs.getString("MarketName"));
			marks.add(mar);
		}
		return marks;
	}
}
